/**
 * brief:   网页响应式布局处理
 * author:  yun
 * date:    2017-02-10
 *
 */

/**
 * 窗口大小改变
 */
window.onresize = function () {
    // var ah = screen.availHeight;
    // var aw = screen.availWidth;
    //
    // var ih = window.innerHeight;
    // var iw = window.innerWidth;
    //
    // var message = document.getElementById("family_message");
    // message.innerHTML = "windows [" + iw + " , " + ih + "]";
    //
    // var status_bar = document.getElementsByClassName("status_bar")[0];
    // message.innerHTML += "&nbsp;&nbsp;&nbsp;手机屏幕";

    PGrefreshLayout();
};

/**
 * 刷新页面布局
 */
function PGrefreshLayout() {
    var width = window.innerWidth;
    var layout = "display";
    if (width < 768) {
        layout = "phone"
    }

    var response = document.getElementsByClassName("responsive");
    for(var i=0, len=response.length; i<len; i++) {
        response[i].setAttribute("layout", layout);
    }
}

/**
 * 页面消息提示
 */
function PGshowNotify(msg, type) {
    var message = document.getElementById("family_message");
    message.innerHTML = msg;

    var t = arguments[1] ? arguments[1] : "info";

    if (t === "err") {
        message.setAttribute("type", "error");
    } else {
        message.setAttribute("type", "info");
    }

}

/**
 * 页面初始化
 * 连接状态
 *
 */
function PGsetStateConn(state) {
    var label = document.getElementById("user_state");

    if (state === "online") {
        label.innerHTML = "在线"
    } else {
        label.innerHTML = "离线"
    }

    var icon = document.getElementsByClassName("member_icon")[0];
    var icon_src = icon.getAttribute("src");
    var path = icon_src.split("/");
    var icon_src_new = "";
    for(var i=0, len=path.length-1; i<len; i++) {
        icon_src_new += path[i] += "/"
    }

    if (state === "online") {
        icon_src_new += "online.png";
    } else {
        icon_src_new += "offline.png";
    }

    icon.setAttribute("src", icon_src_new);
}

/**
 * 设置服务状态
 */
function PGsetSevState(dev, sev, state, desc) {
    var list = document.getElementsByClassName("service_cell");
    var cell = null;
    for(var i=0, len=list.length; i<len; i++) {
        if(list[i].getAttribute("dev") === dev && list[i].getAttribute("sev") === sev) {
            cell = list[i];
            break
        }
    }

    if (cell === null) {
        return;
    }

    // 设置状态
    var desc_info = cell.getElementsByClassName("state")[0];
    desc_info.innerHTML = desc;

    if (cell.getAttribute("state") == state) {
        return;
    }

    cell.setAttribute("state", state);

    // 更新图标
    var icon = cell.getElementsByClassName("icon")[0];
    var icon_src = icon.getAttribute("src");
    var path = icon_src.split("/");
    var icon_src_new = "";
    for(i=0, len=path.length-1; i<len; i++) {
        icon_src_new += path[i] += "/"
    }
    icon_src_new += state += ".png";
    icon.setAttribute("src", icon_src_new);
}